<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>controlrange_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_dom_controlrange_test.js.html">controlrange_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.dom.ControlRangeTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.dom.ControlRangeTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.ControlRange&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.RangeType&#39;);
<a name="line21"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line22"></a>goog.require(&#39;goog.dom.TextRange&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.dom&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line25"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line26"></a>
<a name="line27"></a>var logo;
<a name="line28"></a>var table;
<a name="line29"></a>
<a name="line30"></a>function setUpPage() {
<a name="line31"></a>  logo = goog.dom.getElement(&#39;logo&#39;);
<a name="line32"></a>  table = goog.dom.getElement(&#39;table&#39;);
<a name="line33"></a>}
<a name="line34"></a>
<a name="line35"></a>function testCreateFromElement() {
<a name="line36"></a>  if (!goog.userAgent.IE) {
<a name="line37"></a>    return;
<a name="line38"></a>  }
<a name="line39"></a>  assertNotNull(&#39;Control range object can be created for element&#39;,
<a name="line40"></a>      goog.dom.ControlRange.createFromElements(logo));
<a name="line41"></a>}
<a name="line42"></a>
<a name="line43"></a>function testCreateFromRange() {
<a name="line44"></a>  if (!goog.userAgent.IE) {
<a name="line45"></a>    return;
<a name="line46"></a>  }
<a name="line47"></a>  var range = document.body.createControlRange();
<a name="line48"></a>  range.addElement(table);
<a name="line49"></a>  assertNotNull(&#39;Control range object can be created for element&#39;,
<a name="line50"></a>      goog.dom.ControlRange.createFromBrowserRange(range));
<a name="line51"></a>}
<a name="line52"></a>
<a name="line53"></a>function testSelect() {
<a name="line54"></a>  if (!goog.userAgent.IE) {
<a name="line55"></a>    return;
<a name="line56"></a>  }
<a name="line57"></a>
<a name="line58"></a>  var range = goog.dom.ControlRange.createFromElements(table);
<a name="line59"></a>  range.select();
<a name="line60"></a>
<a name="line61"></a>  assertEquals(&#39;Control range should be selected&#39;, &#39;Control&#39;,
<a name="line62"></a>      document.selection.type);
<a name="line63"></a>  assertEquals(&#39;Control range should have length 1&#39;, 1,
<a name="line64"></a>      document.selection.createRange().length);
<a name="line65"></a>  assertEquals(&#39;Control range should select table&#39;, table,
<a name="line66"></a>      document.selection.createRange().item(0));
<a name="line67"></a>}
<a name="line68"></a>
<a name="line69"></a>function testControlRangeIterator() {
<a name="line70"></a>  if (!goog.userAgent.IE) {
<a name="line71"></a>    return;
<a name="line72"></a>  }
<a name="line73"></a>  var range = goog.dom.ControlRange.createFromElements(logo, table);
<a name="line74"></a>  // Each node is included twice - once as a start tag, once as an end.
<a name="line75"></a>  goog.testing.dom.assertNodesMatch(range, [&#39;#logo&#39;, &#39;#logo&#39;, &#39;#table&#39;,
<a name="line76"></a>    &#39;#tbody&#39;, &#39;#tr1&#39;, &#39;#td11&#39;, &#39;a&#39;, &#39;#td11&#39;, &#39;#td12&#39;, &#39;b&#39;, &#39;#td12&#39;, &#39;#tr1&#39;,
<a name="line77"></a>    &#39;#tr2&#39;, &#39;#td21&#39;, &#39;c&#39;, &#39;#td21&#39;, &#39;#td22&#39;, &#39;d&#39;, &#39;#td22&#39;, &#39;#tr2&#39;, &#39;#tbody&#39;,
<a name="line78"></a>    &#39;#table&#39;]);
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>function testBounds() {
<a name="line82"></a>  if (!goog.userAgent.IE) {
<a name="line83"></a>    return;
<a name="line84"></a>  }
<a name="line85"></a>
<a name="line86"></a>  // Initialize in both orders.
<a name="line87"></a>  helpTestBounds(goog.dom.ControlRange.createFromElements(logo, table));
<a name="line88"></a>  helpTestBounds(goog.dom.ControlRange.createFromElements(table, logo));
<a name="line89"></a>}
<a name="line90"></a>
<a name="line91"></a>function helpTestBounds(range) {
<a name="line92"></a>  assertEquals(&#39;Start node is logo&#39;, logo, range.getStartNode());
<a name="line93"></a>  assertEquals(&#39;Start offset is 0&#39;, 0, range.getStartOffset());
<a name="line94"></a>  assertEquals(&#39;End node is table&#39;, table, range.getEndNode());
<a name="line95"></a>  assertEquals(&#39;End offset is 1&#39;, 1, range.getEndOffset());
<a name="line96"></a>}
<a name="line97"></a>
<a name="line98"></a>function testCollapse() {
<a name="line99"></a>  if (!goog.userAgent.IE) {
<a name="line100"></a>    return;
<a name="line101"></a>  }
<a name="line102"></a>
<a name="line103"></a>  var range = goog.dom.ControlRange.createFromElements(logo, table);
<a name="line104"></a>  assertFalse(&#39;Not initially collapsed&#39;, range.isCollapsed());
<a name="line105"></a>  range.collapse();
<a name="line106"></a>  assertTrue(&#39;Successfully collapsed&#39;, range.isCollapsed());
<a name="line107"></a>}
<a name="line108"></a>
<a name="line109"></a>function testGetContainer() {
<a name="line110"></a>  if (!goog.userAgent.IE) {
<a name="line111"></a>    return;
<a name="line112"></a>  }
<a name="line113"></a>
<a name="line114"></a>  var range = goog.dom.ControlRange.createFromElements(logo);
<a name="line115"></a>  assertEquals(&#39;Single element range is contained by itself&#39;, logo,
<a name="line116"></a>      range.getContainer());
<a name="line117"></a>
<a name="line118"></a>  range = goog.dom.ControlRange.createFromElements(logo, table);
<a name="line119"></a>  assertEquals(&#39;Two element range is contained by body&#39;, document.body,
<a name="line120"></a>      range.getContainer());
<a name="line121"></a>}
<a name="line122"></a>
<a name="line123"></a>function testSave() {
<a name="line124"></a>  if (!goog.userAgent.IE) {
<a name="line125"></a>    return;
<a name="line126"></a>  }
<a name="line127"></a>
<a name="line128"></a>  var range = goog.dom.ControlRange.createFromElements(logo, table);
<a name="line129"></a>  var savedRange = range.saveUsingDom();
<a name="line130"></a>
<a name="line131"></a>  range.collapse();
<a name="line132"></a>  assertTrue(&#39;Successfully collapsed&#39;, range.isCollapsed());
<a name="line133"></a>
<a name="line134"></a>  range = savedRange.restore();
<a name="line135"></a>  assertEquals(&#39;Restored a control range&#39;, goog.dom.RangeType.CONTROL,
<a name="line136"></a>      range.getType());
<a name="line137"></a>  assertFalse(&#39;Not collapsed after restore&#39;, range.isCollapsed());
<a name="line138"></a>  helpTestBounds(range);
<a name="line139"></a>}
<a name="line140"></a>
<a name="line141"></a>function testRemoveContents() {
<a name="line142"></a>  if (!goog.userAgent.IE) {
<a name="line143"></a>    return;
<a name="line144"></a>  }
<a name="line145"></a>
<a name="line146"></a>  var img = goog.dom.createDom(&#39;IMG&#39;);
<a name="line147"></a>  img.src = logo.src;
<a name="line148"></a>
<a name="line149"></a>  var div = goog.dom.getElement(&#39;test1&#39;);
<a name="line150"></a>  div.innerHTML = &#39;&#39;;
<a name="line151"></a>  div.appendChild(img);
<a name="line152"></a>  assertEquals(&#39;Div has 1 child&#39;, 1, div.childNodes.length);
<a name="line153"></a>
<a name="line154"></a>  var range = goog.dom.ControlRange.createFromElements(img);
<a name="line155"></a>  range.removeContents();
<a name="line156"></a>  assertEquals(&#39;Div has 0 children&#39;, 0, div.childNodes.length);
<a name="line157"></a>  assertTrue(&#39;Range is collapsed&#39;, range.isCollapsed());
<a name="line158"></a>}
<a name="line159"></a>
<a name="line160"></a>function testReplaceContents() {
<a name="line161"></a>  // Test a control range.
<a name="line162"></a>  if (!goog.userAgent.IE) {
<a name="line163"></a>    return;
<a name="line164"></a>  }
<a name="line165"></a>
<a name="line166"></a>  var outer = goog.dom.getElement(&#39;test1&#39;);
<a name="line167"></a>  outer.innerHTML =
<a name="line168"></a>      &#39;&lt;div contentEditable=&quot;true&quot;&gt;&#39; +
<a name="line169"></a>      &#39;Hello &lt;input type=&quot;text&quot; value=&quot;World&quot;&gt;&#39; +
<a name="line170"></a>      &#39;&lt;/div&gt;&#39;;
<a name="line171"></a>  range = goog.dom.ControlRange.createFromElements(
<a name="line172"></a>      outer.getElementsByTagName(goog.dom.TagName.INPUT)[0]);
<a name="line173"></a>  goog.dom.ControlRange.createFromElements(table);
<a name="line174"></a>  range.replaceContentsWithNode(goog.dom.createTextNode(&#39;World&#39;));
<a name="line175"></a>  assertEquals(&#39;Hello World&#39;, outer.firstChild.innerHTML);
<a name="line176"></a>}
<a name="line177"></a>
<a name="line178"></a>function testContainsRange() {
<a name="line179"></a>  if (!goog.userAgent.IE) {
<a name="line180"></a>    return;
<a name="line181"></a>  }
<a name="line182"></a>
<a name="line183"></a>  var table2 = goog.dom.getElement(&#39;table2&#39;);
<a name="line184"></a>  var table2td = goog.dom.getElement(&#39;table2td&#39;);
<a name="line185"></a>  var logo2 = goog.dom.getElement(&#39;logo2&#39;);
<a name="line186"></a>
<a name="line187"></a>  var range = goog.dom.ControlRange.createFromElements(logo, table);
<a name="line188"></a>  var range2 = goog.dom.ControlRange.createFromElements(logo);
<a name="line189"></a>  assertTrue(&#39;Control range contains the other control range&#39;,
<a name="line190"></a>      range.containsRange(range2));
<a name="line191"></a>  assertTrue(&#39;Control range partially contains the other control range&#39;,
<a name="line192"></a>      range2.containsRange(range, true));
<a name="line193"></a>
<a name="line194"></a>  range2 = goog.dom.ControlRange.createFromElements(table2);
<a name="line195"></a>  assertFalse(&#39;Control range does not contain the other control range&#39;,
<a name="line196"></a>      range.containsRange(range2));
<a name="line197"></a>
<a name="line198"></a>  range = goog.dom.ControlRange.createFromElements(table2);
<a name="line199"></a>  range2 = goog.dom.TextRange.createFromNodeContents(table2td);
<a name="line200"></a>  assertTrue(&#39;Control range contains text range&#39;,
<a name="line201"></a>      range.containsRange(range2));
<a name="line202"></a>
<a name="line203"></a>  range2 = goog.dom.TextRange.createFromNodeContents(table);
<a name="line204"></a>  assertFalse(&#39;Control range does not contain text range&#39;,
<a name="line205"></a>      range.containsRange(range2));
<a name="line206"></a>
<a name="line207"></a>  range = goog.dom.ControlRange.createFromElements(logo2);
<a name="line208"></a>  range2 = goog.dom.TextRange.createFromNodeContents(table2);
<a name="line209"></a>  assertFalse(&#39;Control range does not fully contain text range&#39;,
<a name="line210"></a>      range.containsRange(range2, false));
<a name="line211"></a>
<a name="line212"></a>  range2 = goog.dom.ControlRange.createFromElements(table2);
<a name="line213"></a>  assertTrue(&#39;Control range contains the other control range (2)&#39;,
<a name="line214"></a>      range2.containsRange(range));
<a name="line215"></a>}
<a name="line216"></a>
<a name="line217"></a>function testCloneRange() {
<a name="line218"></a>  if (!goog.userAgent.IE) {
<a name="line219"></a>    return;
<a name="line220"></a>  }
<a name="line221"></a>  var range = goog.dom.ControlRange.createFromElements(logo);
<a name="line222"></a>  assertNotNull(&#39;Control range object created for element&#39;, range);
<a name="line223"></a>
<a name="line224"></a>  var cloneRange = range.clone();
<a name="line225"></a>  assertNotNull(&#39;Cloned control range object&#39;, cloneRange);
<a name="line226"></a>  assertArrayEquals(&#39;Control range and clone have same elements&#39;,
<a name="line227"></a>      range.getElements(), cloneRange.getElements());
<a name="line228"></a>}
</pre>


</body>
</html>
